package com.veo.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.veo.entity.User;
import com.veo.service.ReaderService;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("readerAction")
@Scope("prototype")
@ParentPackage("json-default")
@Namespace("/")
@Results({
        @Result(name = "success",type = "json",params = {"root","resultMapToReader"}),
        @Result(name = "ok",type = "json",params = {"root","resultMap"}),
})
public class RederAction extends ActionSupport implements ModelDriven<User> {

    private User reader=new User();

    @Autowired
    private ReaderService readerService;
    private List<User> resultMapToReader;

    private Map<String,String> resultMap;

    public List<User> getResultMapToReader() {
        return resultMapToReader;
    }

    public void setResultMapToReader(List<User> resultMapToReader) {
        this.resultMapToReader = resultMapToReader;
    }

    public Map<String, String> getResultMap() {
        return resultMap;
    }

    public void setResultMap(Map<String, String> resultMap) {
        this.resultMap = resultMap;
    }

    @Override
    public User getModel() {
        return reader;
    }


    @Action("getAllReader")
    public String getAllReader(){
        resultMapToReader=readerService.getAllReader();
        return SUCCESS;
    }

    @Action("addReader")
    public String addReader(){
        System.out.println(reader);
        reader.setStatus("1");
        resultMap=new HashMap<String,String>();
        try {
            readerService.addReader(reader);
            resultMap.put("msg","ok");
        }catch (Exception e){
            resultMap.put("msg","error");
            return "ok";
        }
        return "ok";
    }

    @Action("deleteReader")
    public String deleteReader(){
        resultMap=new HashMap<String,String>();
        try {
            readerService.deleteReader(reader);
            resultMap.put("msg","ok");
        }catch (Exception e){
            resultMap.put("msg","error");
            return "ok";
        }
        return "ok";
    }

    @Action("updateReader")
    public String updateReader(){
        resultMap=new HashMap<String,String>();
        try {
            readerService.updateReader(reader);
            resultMap.put("msg","ok");
        }catch (Exception e){
            resultMap.put("msg","error");
            return "ok";
        }
        return "ok";
    }

    @Action("findIdCard")
    public String findIdCard(){
        resultMap=new HashMap<String,String>();
        try {
            boolean flag = readerService.findIdCacr(reader);
            if (flag){
                resultMap.put("msg","true");
            }
            else {
                resultMap.put("msg","false");
            }
        }catch (Exception e){
            resultMap.put("msg","error");
            return "ok";
        }
        return "ok";
    }


    @Action("findUsername")
    public String findUsername(){
        resultMap=new HashMap<String,String>();
        try {
            boolean flag = readerService.findUsername(reader);
            if (flag){
                resultMap.put("msg","true");
            }
            else {
                resultMap.put("msg","false");
            }
        }catch (Exception e){
            resultMap.put("msg","error");
            return "ok";
        }
        return "ok";
    }
}
